

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>geoslurp.dataset.ArcticDEM &mdash; geoslurp v1.1.0 documentation</title>
  

  
  
    <link rel="shortcut icon" href="../../../_static/favicon.ico"/>
  
  
  

  

  
  
    

  

  
  
    <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  

  

  
        <link rel="index" title="Index"
              href="../../../genindex.html"/>
        <link rel="search" title="Search" href="../../../search.html"/>
    <link rel="top" title="geoslurp v1.1.0 documentation" href="../../../index.html"/>
        <link rel="up" title="Module code" href="../../index.html"/> 

  
  <script src="../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav" role="document">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../index.html" class="icon icon-home"> geoslurp
          

          
          </a>

          
            
            
              <div class="version">
                1.1.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../introduction.html">An introduction to Geoslurp</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../installation.html">Installation of the geoslurp package and setting up the PostgreSQL instance</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../confsettings.html">Configuration</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../examples.html">Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../reference.html">Reference Documentation</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">geoslurp</a>
        
      </nav>


      
      <div class="wy-nav-content">
        <div class="rst-content">
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>geoslurp.dataset.ArcticDEM</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for geoslurp.dataset.ArcticDEM</h1><div class="highlight"><pre>
<span></span><span class="c1"># This file is part of geoslurp.</span>
<span class="c1"># geoslurp is free software; you can redistribute it and/or</span>
<span class="c1"># modify it under the terms of the GNU Lesser General Public</span>
<span class="c1"># License as published by the Free Software Foundation; either</span>
<span class="c1"># version 3 of the License, or (at your option) any later version.</span>

<span class="c1"># geoslurp is distributed in the hope that it will be useful,</span>
<span class="c1"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c1"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU</span>
<span class="c1"># Lesser General Public License for more details.</span>

<span class="c1"># You should have received a copy of the GNU Lesser General Public</span>
<span class="c1"># License along with geoslurp; if not, write to the Free Software</span>
<span class="c1"># Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA</span>

<span class="c1"># Author Roelof Rietbroek (roelof@geod.uni-bonn.de), 2019</span>


<span class="kn">from</span> <span class="nn">geoslurp.dataset.OGRBase</span> <span class="k">import</span> <span class="n">OGRBase</span>
<span class="kn">from</span> <span class="nn">geoslurp.dataset.RasterBase</span> <span class="k">import</span> <span class="n">RasterBase</span>
<span class="kn">from</span> <span class="nn">geoslurp.datapull.http</span> <span class="k">import</span> <span class="n">Uri</span> <span class="k">as</span> <span class="n">http</span>
<span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">datetime</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">zipfile</span> <span class="k">import</span> <span class="n">ZipFile</span>
<span class="kn">from</span> <span class="nn">geoslurp.config.slurplogger</span> <span class="k">import</span> <span class="n">slurplogger</span>
<span class="kn">from</span> <span class="nn">geoslurp.config.catalogue</span> <span class="k">import</span> <span class="n">geoslurpCatalogue</span>
<span class="c1"># from geoalchemy2.types import Raster</span>
<span class="c1"># from sqlalchemy.ext.declarative import declared_attr, as_declarative</span>
<span class="c1"># from sqlalchemy import Column, Integer, String, MetaData</span>
<span class="c1"># from geoslurp.datapull import findFiles</span>
<span class="c1"># from geoslurp.datapull.uri import UriFile</span>
<span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="k">import</span> <span class="n">func</span>

<span class="c1"># @as_declarative(metadata=MetaData(schema=&#39;dem&#39;))</span>
<span class="c1"># class ArcticRasterTBase(object):</span>
<span class="c1">#     @declared_attr</span>
<span class="c1">#     def __tablename__(cls):</span>
<span class="c1">#         #strip of the &#39;Table&#39; from the class name</span>
<span class="c1">#         return cls.__name__[:-5].lower()</span>
<span class="c1">#     id = Column(Integer, primary_key=True)</span>
<span class="c1">#     tile=Column(String)</span>
<span class="c1">#     rast=Column(Raster)</span>

<div class="viewcode-block" id="Arcticdemindex"><a class="viewcode-back" href="../../../reference/geoslurp.dataset.html#geoslurp.dataset.ArcticDEM.Arcticdemindex">[docs]</a><span class="k">class</span> <span class="nc">Arcticdemindex</span><span class="p">(</span><span class="n">OGRBase</span><span class="p">):</span>
    <span class="n">scheme</span><span class="o">=</span><span class="s1">&#39;dem&#39;</span>
    <span class="n">targetsrid</span><span class="o">=</span><span class="mi">3413</span>
    <span class="n">filebase</span><span class="o">=</span><span class="s2">&quot;ArcticDEM_Tile_Index_Rel7&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">dbconn</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">dbconn</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ogrfile</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cacheDir</span><span class="p">(),</span><span class="s1">&#39;extract&#39;</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">filebase</span><span class="o">+</span><span class="s2">&quot;.shp&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="s2">&quot;ArcticDEMversion&quot;</span>  <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_dbinvent</span><span class="o">.</span><span class="n">data</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_dbinvent</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;ArcticDEMversion&quot;</span><span class="p">]</span><span class="o">=</span><span class="nb">tuple</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_dbinvent</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;ArcticDEMversion&quot;</span><span class="p">])</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_dbinvent</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;ArcticDEMversion&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="mi">7</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>

<div class="viewcode-block" id="Arcticdemindex.pull"><a class="viewcode-back" href="../../../reference/geoslurp.dataset.html#geoslurp.dataset.ArcticDEM.Arcticdemindex.pull">[docs]</a>    <span class="k">def</span> <span class="nf">pull</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Pulls the shapefile layers from the server&quot;&quot;&quot;</span>
        <span class="n">zipf</span><span class="o">=</span><span class="n">http</span><span class="p">(</span><span class="s2">&quot;http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/indexes/&quot;</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">filebase</span><span class="o">+</span><span class="s2">&quot;.zip&quot;</span><span class="p">,</span><span class="n">lastmod</span><span class="o">=</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2018</span><span class="p">,</span><span class="mi">9</span><span class="p">,</span><span class="mi">26</span><span class="p">))</span>

        <span class="c1">#download the zip shapefiles</span>
        <span class="n">downloaddir</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">cacheDir</span><span class="p">()</span>
        <span class="n">uri</span><span class="p">,</span><span class="n">upd</span><span class="o">=</span><span class="n">zipf</span><span class="o">.</span><span class="n">download</span><span class="p">(</span><span class="n">downloaddir</span><span class="p">,</span><span class="n">check</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="n">zipd</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">downloaddir</span><span class="p">,</span><span class="s1">&#39;extract&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">zipd</span><span class="p">):</span>
            <span class="c1">#unzip the goodies</span>
            <span class="k">with</span> <span class="n">ZipFile</span><span class="p">(</span><span class="n">uri</span><span class="o">.</span><span class="n">url</span><span class="p">,</span><span class="s1">&#39;r&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">zp</span><span class="p">:</span>
                <span class="n">slurplogger</span><span class="p">()</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Unzipping </span><span class="si">%s</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">uri</span><span class="o">.</span><span class="n">url</span><span class="p">))</span>
                <span class="n">zp</span><span class="o">.</span><span class="n">extractall</span><span class="p">(</span><span class="n">zipd</span><span class="p">)</span></div></div>



<span class="c1"># def ArcticDEMMetaExtractor(uri):</span>
<span class="c1">#     slurplogger().info(&quot;Extracting info from raster: %s&quot;%(uri.url))</span>
<span class="c1">#     with open(uri.url,&#39;rb&#39;) as fid:</span>
<span class="c1">#         fbytes=fid.read()</span>
<span class="c1">#     meta={&quot;rast&quot;:func.ST_FromGDALRaster(fbytes)}</span>
<span class="c1">#     return meta</span>

<div class="viewcode-block" id="ArcticDemRasterBase"><a class="viewcode-back" href="../../../reference/geoslurp.dataset.html#geoslurp.dataset.ArcticDEM.ArcticDemRasterBase">[docs]</a><span class="k">class</span> <span class="nc">ArcticDemRasterBase</span><span class="p">(</span><span class="n">RasterBase</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;&quot;Base class to download/register Arctic DEM Tiff rasters&quot;&quot;&quot;</span>
    <span class="n">scheme</span><span class="o">=</span><span class="s2">&quot;dem&quot;</span>
    <span class="n">res</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">rasterfile</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">rastregex</span> <span class="o">=</span> <span class="s1">&#39;.*\.tif$&#39;</span>
    <span class="n">srid</span><span class="o">=</span><span class="mi">3413</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">dbconn</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">dbconn</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">rasterfile</span><span class="o">=</span><span class="s2">&quot;arcticdem_mosaic_&quot;</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">res</span><span class="o">+</span><span class="s2">&quot;_v3.0.tif&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_dbinvent</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="s2">&quot;Description&quot;</span><span class="p">]</span><span class="o">=</span><span class="s2">&quot;ArcticDEM raster table&quot;</span>

<div class="viewcode-block" id="ArcticDemRasterBase.pull"><a class="viewcode-back" href="../../../reference/geoslurp.dataset.html#geoslurp.dataset.ArcticDEM.ArcticDemRasterBase.pull">[docs]</a>    <span class="k">def</span> <span class="nf">pull</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">intersect</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="c1"># download the entire mosaic domain in one tif</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">res</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;1km&#39;</span><span class="p">,</span><span class="s1">&#39;500m&#39;</span><span class="p">,</span><span class="s1">&#39;100m&#39;</span><span class="p">]:</span>
            <span class="n">rasteruri</span><span class="o">=</span><span class="n">http</span><span class="p">(</span><span class="s2">&quot;http://data.pgc.umn.edu/elev/dem/setsm/ArcticDEM/mosaic/v3.0/&quot;</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">res</span><span class="o">+</span><span class="s2">&quot;/&quot;</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">rasterfile</span><span class="p">,</span><span class="n">lastmod</span><span class="o">=</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2018</span><span class="p">,</span><span class="mi">9</span><span class="p">,</span><span class="mi">26</span><span class="p">))</span>
            <span class="n">rasterfileuri</span><span class="p">,</span><span class="n">upd</span><span class="o">=</span><span class="n">rasteruri</span><span class="o">.</span><span class="n">download</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">srcdir</span><span class="p">,</span><span class="n">check</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span></div></div>

        <span class="c1">#download only those tiles which are needed</span>


<div class="viewcode-block" id="getArcticDems"><a class="viewcode-back" href="../../../reference/geoslurp.dataset.html#geoslurp.dataset.ArcticDEM.getArcticDems">[docs]</a><span class="k">def</span> <span class="nf">getArcticDems</span><span class="p">(</span><span class="n">conf</span><span class="p">):</span>
    <span class="n">out</span><span class="o">=</span><span class="p">[]</span>
    <span class="k">for</span> <span class="n">res</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;1km&#39;</span><span class="p">,</span> <span class="s1">&#39;500m&#39;</span><span class="p">,</span> <span class="s1">&#39;100m&#39;</span><span class="p">]:</span>
        <span class="n">out</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="s2">&quot;arcticdem_mosaic_&quot;</span><span class="o">+</span><span class="n">res</span><span class="o">+</span><span class="s2">&quot;_v3&quot;</span><span class="p">,</span> <span class="p">(</span><span class="n">ArcticDemRasterBase</span><span class="p">,),</span> <span class="p">{</span><span class="s2">&quot;res&quot;</span><span class="p">:</span><span class="n">res</span><span class="p">,</span><span class="s2">&quot;tiles&quot;</span><span class="p">:[</span><span class="mi">100</span><span class="p">,</span><span class="mi">100</span><span class="p">]}))</span>
        <span class="c1"># out.append(type(&quot;arcticdem_mosaic_&quot;+res+&quot;_v3&quot;, (ArcticDemRasterBase,), {&quot;res&quot;:res}))</span>

    <span class="k">return</span> <span class="n">out</span></div>


<span class="c1">#register datasets</span>
<span class="n">geoslurpCatalogue</span><span class="o">.</span><span class="n">addDataset</span><span class="p">(</span><span class="n">Arcticdemindex</span><span class="p">)</span>
<span class="n">geoslurpCatalogue</span><span class="o">.</span><span class="n">addDatasetFactory</span><span class="p">(</span><span class="n">getArcticDems</span><span class="p">)</span>

</pre></div>

           </div>
           <div class="articleComments">
            
           </div>
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, Roelof Rietbroek.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../',
            VERSION:'v1.1.0',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../_static/doctools.js"></script>

  

  
  
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>
  

  
  
  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.StickyNav.enable();
      });
  </script>
   

</body>
</html>